package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.dto.UserLoginMsg;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xD
 * @since 2023/01/10 09:26
 */
@RestController
@RequestMapping("user")
public class LoginController {

    @Resource
    private ILoginService iLoginService;

    @Resource
    private IKaptchaService iKaptchaService;

    /**
     * 需要验证token
     *
     * @since 2023/01/10 9:28
     */
    @GetMapping("login")
    public ResponseData login(HttpServletRequest request) {
        String token = CookieUtil.getCookieValue(request, "access_token");
        //若获取的cookie为空 则抛出异常,由ExceptionHandler响应
        if (StringUtils.isEmpty(token)) {
            throw new ValidateException(SysRetCodeConstants.GET_TOKEN_FAILED.getCode(), SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());
        }
        UserLoginMsg userLoginMsg = iLoginService.verifyToken(token);
        //不为空,则验证token是否正确
        return new ResponseUtil<>().setData(userLoginMsg);
    }

    /**
     * 不需要验证token
     *
     * @since 2023/01/10 9:28
     */
    @PostMapping("login")
    public ResponseData login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request, HttpServletResponse response) {
        //校验验证码
        // 1) 从cookie中获取uuid
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(userLoginRequest.getCaptcha());
        // 2) 若出现异常则由ControllerExceptionHandler响应请求
        iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        //校验用户名密码,邮箱
        UserLoginResponse loginResponse = iLoginService.login(userLoginRequest);
        //响应cookie 并设置时间
        String token = loginResponse.getToken();
        Cookie cookie = new Cookie("access_token", token);
        // 1) 设置cookie失效时间
        cookie.setMaxAge(60 * 60 * 24);
        // 2) 设置cookie应用内共享
        cookie.setPath("/");
        CookieUtil.setCookie(response, cookie);
        return new ResponseUtil<>().setData(loginResponse);
    }

    @GetMapping("loginOut")
    public ResponseData logout(HttpServletResponse response, HttpServletRequest request) {
        //通过jwt工具类将Cookie中的token设置为到期
        String token = CookieUtil.getCookieValue(request, "access_token");
        Cookie cookie = new Cookie("access_token", token);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        CookieUtil.setCookie(response, cookie);
        return new ResponseUtil<>().setData(null,"success");
    }
}
